unit LT_Task;

interface
uses Windows,Classes,Sysutils,PluginElems,Dialogs,LT_TaskHistory;
  const
		STATE_TASK_NOT_BEGIN = 0;
		STATE_TASK_COMPLETED = 1;
		STATE_LINK_TYPE = 2;
		STATE_NOTE = 3;
		STATE_TASK_IN_WORK = 4;
		STATE_TASK_READY = 5;
		STATE_TASK_PAUSED = 6;
		STATE_TASK_CANCELLED = 7;
  type
  PEachTaskProcedure=^TEachTaskProcedure;
  PLT_Task=^TLT_Task;
  TLT_Task=class
  private
    Fuid:TLT_UID;
    FTitle:PWideChar;
    FParentUID:PWideChar;
    FCreateDate,FStartDate,FEndDate:TDateTime;
    FState:Integer;
    FComment:PWideChar;


    function isExists():Boolean;
    function getCreateDate: TDateTime;
    function getModifyDate: TDateTime;
  public
    history:TLT_TaskHistory;
    constructor Create(uid: TLT_UID=nil);
    function isPresent():Boolean;
    //children
    function getChildsCount():Integer;
    function getChildUID(n:integer):TLT_UID;
    function getChildrenTasks(withSubTasks:Boolean):TList;
    //parent
    function getParentUID():TLT_UID;
    function setParentUID(parentUID:TLT_UID):Boolean;
    //state
    function getState(): Integer;
    procedure setState(state: Integer);
    //title
    function getTitle():PWideChar;
    procedure setTitle(text:PWideChar);
    //dates
    function getStartDate():TDateTime;
    function getEndDate():TDateTime;
    function setDates(startDate:TDateTime; endDate:TDateTime):Boolean;
//    function GetDateCreate():TDateTime;
    procedure SetDateCreate(date:TDateTime);
    //comment
    function getComment():PWideChar;
    procedure setComment(comment:PWideChar);
    //priority
    function getPriority():PWideChar;
    function setPriority(priority:PWideChar):Boolean;
    //order
    function getOrder():Integer;
    function setOrder(order:Integer):Boolean;
    //reminder
    function getDateReminder():TDateTime;
    function setDateReminder(date:TdateTime):Boolean;
    //category
    function GetCategoriesCount():Integer;
    function GetCategoryUID(n:Integer):TLT_UID;
    function AddToCategory(catId:TLT_UID):Boolean;
    function RemoveFromCategory(catId:TLT_UID):Boolean;
    //project
    function GetProjectsCount():Integer;
    function GetProjectsUID(n:Integer):TLT_UID;
    function AddToProject(projectUID:TLT_UID):Boolean;
    function RemoveFromProject(projectUID:TLT_UID):Boolean;


    //operations
    procedure setUID(uid:TLT_UID);
    function Save():Boolean;
    function Delete():Boolean;
    procedure EachSubTask(callback:Pointer; list:TList; withSubTasks:Boolean);
    //function Insert();
    //function Insert(uid: TLT_UID;parentUID:TLT_UID;title:PWideChar;createDate:TDateTime;state: Integer;comment: PWideChar;startDate:TDateTime;endDate: TDateTime

    property uid:TLT_UID read Fuid write setUID;
    property parentUID:TLT_UID read getParentUID;
    property title:PWideChar read GetTitle write SetTitle;
    property createDate:TDateTime read getCreateDate;
    property state:integer read getState write setState;
    property comment:PWideChar read getComment write setComment;
    property startDate:TDateTime read getStartDate;
    property endDate:TDateTime read getEndDate;
    property exists:Boolean read isExists;
    property modifyDate:TDateTime read getModifyDate;

  end;
  TEachTaskProcedure=function(task:TLT_Task):Boolean;
implementation

{ TLT_Task }


constructor TLT_Task.Create(uid: TLT_UID=nil);
begin
  FParentUID:='';
  FCreateDate:=getTime();
  FState:=0;
  FComment:='';
  FStartDate:=AddinLT_GetMinDate();
  FEndDate:=AddinLT_GetMaxDate();
  setUId(uid);
end;

function TLT_Task.Delete: Boolean;
begin
  Result:=AddinLT_Task_Delete(uid);
end;

function TLT_Task.getChildsCount: Integer;
var c:Integer;
begin
  c:=AddinLT_Task_GetChildsCount(uid);
  Result:=c;
end;

function TLT_Task.getChildUID(n: integer): TLT_UID;
begin
Result:=AddinLT_Task_GetChildUID(uid,n);
end;

function TLT_Task.getEndDate: TDateTime;
begin
  if exists then
    Result:=AddinLT_Task_GetDateEnd(uid)
  else
    Result:=FEndDate;
end;

function TLT_Task.getParentUID: TLT_UID;
begin
if exists then
  Result:=AddinLT_Task_GetParentUID(uid)
else
  Result:=FParentUID;
end;

function TLT_Task.getComment: PWideChar;
begin
if exists then
  Result:=AddinLT_Task_GetRTFComment(uid)
else
  Result:=FComment;
end;

function TLT_Task.getStartDate: TDateTime;
begin
if exists then
  Result:=AddinLT_Task_GetDateBegin(uid)
else
  Result:=FStartDate;
end;

function TLT_Task.getState: Integer;
begin
if exists then
  Result:=AddinLT_Task_GetState(uid)
else
  Result:=FState;
end;

function TLT_Task.getTitle: PWideChar;
begin
if exists then
  Result:=AddinLT_Task_GetTitle(uid)
else
  Result:=FTitle;
end;

function TLT_Task.isExists: Boolean;
begin
  Result:=(uid<>nil) and (WideCharToString(uid)<>'') and isPresent();
end;

function TLT_Task.isPresent: Boolean;
begin
  Result:=AddinLT_Task_IsPresent(uid);
end;

function TLT_Task.Save:Boolean;
var
  id:TLT_UID;
begin
  if exists then
    Result:=AddinLT_Task_SaveChanges(uid)
  else
  begin
    id:=AddinLT_Task_Insert('', getParentUID(), getTitle(), getCreateDate(),getState(),getComment(),getStartDate(),getEndDate());
    Result:=id<>nil;
    if Result then
      setUID(id);

  end;
end;

function TLT_Task.setDates(startDate, endDate: TDateTime): Boolean;
begin
if exists then
  Result:=AddinLT_Task_SetDates(uid, startDate, endDate)
else
begin
  FStartDate:=startDate;
  FEndDate:=endDate;
  Result:=true;
end;
end;


function TLT_Task.setParentUID(parentUID: TLT_UID): Boolean;
begin
if exists then
  Result:=AddinLT_Task_SetParentUID(uid, parentUID)
else
begin
  FParentUID:=parentUID;
  Result:=true;
end;
end;



procedure TLT_Task.setComment(comment: PWideChar);
begin
if exists then
  AddinLT_Task_SetRTFComment(uid,comment)
else
  FComment:=comment;
end;

procedure TLT_Task.setState(state: Integer);
begin
if exists then
  AddinLT_Task_SetState(uid, state)
else
  FState:=state;
end;

procedure TLT_Task.setTitle(text: PWideChar);
begin
if exists then
begin
  AddinLT_Task_SetTitle(uid,text);
end
else
begin
  Ftitle:=text;
end;
end;

function TLT_Task.getPriority: PWideChar;
begin
Result:=AddinLT_Task_GetPriorityUID(uid);
end;

function TLT_Task.setPriority(priority: PWideChar): Boolean;
begin
Result:=AddinLT_Task_SetPriorityUID(uid,priority);
end;

function TLT_Task.getOrder: Integer;
begin
Result:=AddinLT_Task_GetOrder(uid);
end;

function TLT_Task.setOrder(order: Integer): Boolean;
begin
Result:=AddinLT_Task_SetOrder(uid, order);
end;

function TLT_Task.getDateReminder: TDateTime;
begin
Result:=AddinLT_Task_GetDateReminder(uid);
end;

function TLT_Task.setDateReminder(date: TdateTime): Boolean;
begin
Result:=AddinLT_Task_SetDateReminder(uid, date);
end;

function TLT_Task.GetCategoriesCount: Integer;
begin
Result:=AddinLT_Task_GetCategoriesCount(uid);
end;

function TLT_Task.GetCategoryUID(n: Integer): TLT_UID;
begin
Result:=AddinLT_Task_GetCategoryUID(uid, n);
end;

function TLT_Task.AddToCategory(catId: TLT_UID): Boolean;
begin
Result:=AddinLT_Task_AddToCategory(uid,catId);
end;

function TLT_Task.RemoveFromCategory(catId: TLT_UID): Boolean;
begin
Result:=AddinLT_Task_RemoveFromCategory(uid, catid);
end;

function TLT_Task.GetProjectsCount: Integer;
begin
Result:=AddinLT_Task_GetProjectsCount(uid);
end;

function TLT_Task.GetProjectsUID(n: Integer): TLT_UID;
begin
Result:=AddinLT_Task_GetProjectsUID(uid, n);
end;

function TLT_Task.AddToProject(projectUID: TLT_UID): Boolean;
begin
Result:=AddinLT_Task_AddToProject(uid,projectUID);
end;

function TLT_Task.RemoveFromProject(projectUID: TLT_UID): Boolean;
begin
Result:=AddinLT_Task_RemoveFromProject(uid,projectUID);
end;

function TLT_Task.getCreateDate: TDateTime;
begin
if exists then
  Result:=AddinLT_Task_GetDateCreate(uid)
else
  Result:=FCreateDate;
end;

procedure TLT_Task.SetDateCreate(date: TDateTime);
begin
  FCreateDate:=date;
end;

function TLT_Task.getChildrenTasks(withSubTasks:Boolean): TList;
var
list,subList:TList;
i,o:Integer;
task:TLT_Task;
begin
  list:=TList.Create();
  for i:=0 to getChildsCount-1 do
  begin
    task:=TLT_Task.Create(getChildUID(i));
    list.Add(task);
    if withSubTasks then
    begin
      subList:=task.getChildrenTasks(withSubTasks);
      for o:=0 to subList.Count-1 do
        list.Add(subList[o]);
    end;
  end;
  Result:=list;
end;

procedure TLT_Task.EachSubTask(callback: Pointer;list:TList; withSubTasks:Boolean);
var
filterProc:TEachTaskProcedure;
i,count:Integer;
task:TLT_Task;
begin

  @filterProc:=callback;
  task:=TLT_Task.Create();
  count:=getChildsCount;
  //if count<>0 then showMessage(title);
  for i:=0 to count-1 do
  begin
    task.setUId(getChildUID(i));
    if filterProc(task) then list.Add(TLT_Task.Create(task.uid));
    if withSubTasks then
      task.EachSubTask(callback, list,  withSubTasks);
  end;

end;

procedure TLT_Task.setUID(uid: TLT_UID);
begin
  FUID:=uid;
  history:=TLT_TaskHistory.Create(uid,false);
end;














function TLT_Task.getModifyDate: TDateTime;
begin
  if isExists then
    Result:=AddinLT_Task_GetDateModification(uid);
end;


end.
